using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.Base;
using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
   [TestFixture]
   public  class TestDMClassSelectorControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();

        }
        [Ignore("Changing how this works")] 
        [Test]
        public void Test_Construction()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DMClassSelectorControl control =
                new DMClassSelectorControl(new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.AreEqual(1, control.Controls.Count);
            Assert.IsInstanceOf(typeof(IGridControl), control.Controls[0]);
            IGridControl readGridControl = (IGridControl) control.Controls[0];
            IButtonGroupControl buttonsControl = readGridControl.Buttons;
            Assert.IsTrue(buttonsControl.Visible);
            Assert.IsFalse(buttonsControl["Add"].Visible);
            Assert.IsFalse(buttonsControl["Edit"].Visible);
            Assert.IsFalse(buttonsControl["Delete"].Visible);
            Assert.IsNotNull(buttonsControl["New"]);
            Assert.IsNotNull(buttonsControl["Cancel"]);
            Assert.IsNotNull(buttonsControl["Remove"]);
        }

        [Test]
        public void Test_GridControlDoesNotShowButtons()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DMClassSelectorControl control =
                new DMClassSelectorControl(new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.AreEqual(1, control.Controls.Count);
            Assert.IsInstanceOf(typeof(IGridControl), control.Controls[0]);
            IGridControl gridControl = (IGridControl)control.Controls[0];
            IButtonGroupControl buttonsControl = gridControl.Buttons;
            Assert.IsFalse(buttonsControl.Visible);
        }

        [Test]
        public void TestSetDMAssembly()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            DMClassSelectorControl control = new DMClassSelectorControl(new ControlFactoryWin());
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Assembly = assembly;
            //---------------Test Result -----------------------
            Assert.AreSame(assembly, control.Assembly);
            Assert.AreEqual(assembly.Classes.Count, control.Classes.Count);
        }

        [Test]
        public void TestDisabledAtCreation()
        {
            //---------------Execute Test ----------------------
            DMClassSelectorControl control =
                new DMClassSelectorControl(new ControlFactoryWin());
            //---------------Test Result -----------------------

            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void TestSetEnables()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            DMClassSelectorControl control = new DMClassSelectorControl(new ControlFactoryWin());

            //---------------Execute Test ----------------------
            control.Assembly = assembly;
            //---------------Test Result -----------------------
            Assert.IsTrue(control.Enabled);
        }

        [Test]
        public void TestSetToNullDisables()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            DMClassSelectorControl control = new DMClassSelectorControl(new ControlFactoryWin());
            control.Assembly = assembly;
            //---------------Execute Test ----------------------
            control.Assembly = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(control.Enabled);
            Assert.AreEqual(0, control.Classes.Count);

        }

        [Test]
        public void TestSetBusinessObject()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            DMClass dmClass = assembly.CreateClass("Class1");
            DMClassSelectorControl control = new DMClassSelectorControl(new ControlFactoryWin());
            control.Assembly = assembly;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, control.Class);
            Assert.AreSame(control.Class, control.GridControl.SelectedBusinessObject);
        }
        
       [Ignore("Changing how this works")] 
        [Test]
        public void TestBusinessObjectSelectedEvent()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            DMClass dmClass = assembly.CreateClass("Class1");
            DMClassSelectorControl control =
                           new DMClassSelectorControl(new ControlFactoryWin());
            bool fired = false;
            DMClass selected = null;
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            control.ClassSelected +=
                (sender, e) =>
                {
                    fired = true;
                    selected = e.BusinessObject;
                };
            control.Class = dmClass;
            //---------------Test Result -----------------------

            Assert.IsTrue(fired);
            Assert.AreSame(selected, control.Class);
            //---------------Tear Down -------------------------          
        }
        [Ignore("Changing how this works")] 
        [Test]
        public void TestNewButtonClickedCreatesBO_EmptyCollection()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin()); 
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            //  ---------------Assert Precondition----------------
            Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
            BusinessObjectCollection<DMClass> classes = assembly.Classes;
            Assert.AreEqual(0, classes.Count);
            //  ---------------Execute Test ----------------------
            readGridControl.Buttons["New"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
            Assert.AreEqual(1, classes.Count);
            Assert.AreSame(classes[0], readGridControl.SelectedBusinessObject);
        }
        [Ignore("Changing how this works")] 
        [Test]
        public void TestNewButtonClickedCreatesBO_ExistingCollection()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            TestUtilsShared.CreateSavedDMClass(assembly);
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin());
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            //  ---------------Assert Precondition----------------
            Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
            BusinessObjectCollection<DMClass> classes = assembly.Classes;
            Assert.AreEqual(1, classes.Count);
            //  ---------------Execute Test ----------------------
            readGridControl.Buttons["New"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.AreEqual(2, readGridControl.Grid.Rows.Count);
            Assert.AreEqual(2, classes.Count);
            Assert.AreSame(classes[1], readGridControl.SelectedBusinessObject);
        }
        [Ignore("Changing how this works")] 
        [Test]
        public void TestDeleteButtonDeletesCurrentBO()
        {
            // ---------------Set up test pack-------------------
             //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            TestUtilsShared.CreateSavedDMClass(assembly);
            BusinessObjectCollection<DMClass> classes = assembly.Classes;
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin());
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            IBusinessObject currentBO = readGridControl.SelectedBusinessObject;
            //  ---------------Assert Precondition----------------
            Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
            Assert.AreEqual(1, classes.Count);
            //  ---------------Execute Test ----------------------
            readGridControl.Buttons["Remove"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.IsTrue(currentBO.Status.IsDeleted);
            Assert.IsFalse(currentBO.Status.IsDirty);
            Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
            Assert.AreEqual(0, classes.Count);
        }
        [Ignore("Changing how this works")] 
        [Test]
        public void TestDeleteButton_NoCurrentBO()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            BusinessObjectCollection<DMClass> classes = assembly.Classes;
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin());
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            IBusinessObject currentBO = readGridControl.SelectedBusinessObject;
            //  ---------------Assert Precondition----------------
            Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
            Assert.AreEqual(0, classes.Count);
            Assert.IsNull(currentBO);
            //  ---------------Execute Test ----------------------
            readGridControl.Buttons["Remove"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
            Assert.AreEqual(0, classes.Count);
        }
        [Ignore("Changing how this works")] 
        [Test]
        public void TestCancelButton_ClickRestoresSavedObject()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            TestUtilsShared.CreateSavedDMClass(assembly);
            BusinessObjectCollection<DMClass> classes = assembly.Classes;
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin());
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            DMClass currentBO = (DMClass) readGridControl.SelectedBusinessObject;
            currentBO.DisplayName = TestUtilsShared.GetRandomString();
            //  ---------------Assert Precondition----------------
            Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
            Assert.AreEqual(1, classes.Count);
            Assert.IsNotNull(currentBO);
            Assert.IsTrue(currentBO.Status.IsDirty);
            //  ---------------Execute Test ----------------------
            readGridControl.Buttons["Cancel"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.IsFalse(currentBO.Status.IsDirty);
        }
        [Ignore("Changing how this works")] 
        [Test]
        public void TestCancelButton_Click_NoObjecctSelected()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            BusinessObjectCollection<DMClass> classes = assembly.Classes;
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin());
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            DMClass currentBO = (DMClass) readGridControl.SelectedBusinessObject;
            //  ---------------Assert Precondition----------------
            Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
            Assert.AreEqual(0, classes.Count);
            Assert.IsNull(currentBO);
            //  ---------------Execute Test ----------------------
            readGridControl.Buttons["Cancel"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
            Assert.AreEqual(0, classes.Count);
        }
        [Ignore("Changing how this works")] 
        [Test]
        public void TestCancelButton_ClickRemovesNewObject_OnlyItemInGrid()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            TestUtilsShared.CreateSavedDMClass(assembly);
            BusinessObjectCollection<DMClass> classes = assembly.Classes;
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin());
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            IButton cancelButton = readGridControl.Buttons["Cancel"];
            IButton newButton = readGridControl.Buttons["New"];

            newButton.PerformClick();
            //---------------Assert Precondition----------------
            Assert.IsTrue(cancelButton.Enabled);
            Assert.AreEqual(2, readGridControl.Grid.Rows.Count);
            //---------------Execute Test ----------------------
            cancelButton.PerformClick();
            //---------------Test Result -----------------------
            Assert.AreEqual(1, classes.Count, "The new item should be removed from the collection");
            Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
        }

        [Ignore("Changing how this works")] 
        [Test]
        public void TestCancelButton_ClickRemovesNewObject_TwoItemsInGrid()
        {
            // ---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            TestUtilsShared.CreateSavedDMClass(assembly);
            TestUtilsShared.CreateSavedDMClass(assembly);
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin());
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            IButton cancelButton = readGridControl.Buttons["Cancel"];
            IButton newButton = readGridControl.Buttons["New"];
            newButton.PerformClick();
            // ---------------Assert Precondition----------------
            Assert.AreEqual(3, readGridControl.Grid.Rows.Count);
            //   ---------------Execute Test ----------------------
            cancelButton.PerformClick();
            //  ---------------Test Result -----------------------
            Assert.AreEqual(2, readGridControl.Grid.Rows.Count);
            Assert.IsNotNull(readGridControl.SelectedBusinessObject);
        }
        [Ignore("Changing how this works")] 
        [Test]
        public void Test_ObjectSavesWhenSaveButtonClicked()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            TestUtilsShared.CreateSavedDMClass(assembly);
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin());
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            readGridControl.Buttons["New"].PerformClick();
            DMClass currentBO = (DMClass)readGridControl.SelectedBusinessObject;
            TestUtilsShared.UpdateBusinessObjectWithValidData(currentBO);
            currentBO.DisplayName = TestUtilsShared.GetRandomString();

            //---------------Assert Precondition----------------
            Assert.IsNotNull(currentBO);
            Assert.IsTrue(currentBO.Status.IsNew);
            Assert.IsTrue(currentBO.Status.IsDirty);
            Assert.IsTrue(currentBO.Status.IsValid());
            //  ---------------Execute Test ----------------------
            readGridControl.Buttons["Save"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.IsFalse(currentBO.Status.IsDirty);
            Assert.IsFalse(currentBO.Status.IsNew);
            Assert.IsFalse(currentBO.Status.IsDeleted);
        }
        [Ignore("Changing how this works")] 
        [Test]
        public void Test_SaveButtonClicked_NoObjectSelected()
        {
            //---------------Set up test pack-------------------
            DMAssembly assembly = TestUtilsShared.CreateDMAssembly();
            DMClassSelectorControl classSelectorControl =
                new DMClassSelectorControl(new ControlFactoryWin());
            IGridControl readGridControl = classSelectorControl.GridControl;
            classSelectorControl.Assembly = assembly;
            DMClass currentBO = (DMClass)readGridControl.SelectedBusinessObject;
            //---------------Assert Precondition----------------
            Assert.IsNull(currentBO);
            Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
            //  ---------------Execute Test ----------------------
            readGridControl.Buttons["Save"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
        }
    }
}
